package agency.sales;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import agency.Sales;
import dict.AgencyDict;
import dict.BcDict;
import entities.TBank;
import entities.TBc;
import entities.TCard;
import entities.TTransaction;
import util.HttpUtil;
import util.LogUtil;
import utils.AesUtil;
import utils.DateUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class Gloedc implements Sales {

	private static String PATH = "https://gpsgh.gateway.mastercard.com/api/nvp/version/45";
	private static String CURRENCY = "CNY";
	private static String OPERATION = "PAY";
	private static String TYPE = "CARD";

	public void process(TBc bc, TTransaction transaction, TBank bank, TCard card) {
		Map<String, String> requestMap = new TreeMap<String, String>();
		Map<String, String> responseMap = new TreeMap<String, String>();
		String res = null;
		String[] params = null;
		// 消费

		requestMap.put("apiUsername", "merchant." + bank.getBnkMer());
		requestMap.put("apiPassword", bank.getBnkKey());
		requestMap.put("apiOperation", OPERATION);
		requestMap.put("merchant", bank.getBnkMer());
		requestMap.put("order.amount", NumberUtil.transfer(bc.getBcAmount()));
		requestMap.put("order.currency", CURRENCY);
		requestMap.put("order.id", bc.getBcNo());
		requestMap.put("order.reference", transaction.getTxnOrder());
		requestMap.put("sourceOfFunds.type", TYPE);
		try {
			String number = card.getCrdNumber();
			String expire = AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdExpire());
			String cvv = AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdCvv());
			requestMap.put("sourceOfFunds.provided.card.number", number);
			requestMap.put("sourceOfFunds.provided.card.expiry.year", expire.substring(2, 4));
			requestMap.put("sourceOfFunds.provided.card.expiry.month", expire.substring(0, 2));
			requestMap.put("sourceOfFunds.provided.card.securityCode", cvv);
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.GLO_ENCRYPT_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_ENCRYPT_EXCEPTION));
			return;
		}
		requestMap.put("transaction.id", transaction.getTxnNo());
		requestMap.put("transaction.reference", card.getCrdNo());

		// 日志打印发送数据
		StringBuffer send = new StringBuffer();
		for (Map.Entry<String, String> e : requestMap.entrySet()) {
			send.append(e.getKey() + "=" + e.getValue() + "&");
		}
		LogUtil.agency("GLOEDC_SALES", send.toString());

		try {
			res = new HttpUtil().post(PATH, requestMap);
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.GLO_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_TRANSMISSION_EXCEPTION));
			return;
		}

		if (StringUtil.isEmpty(res)) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data response exception");
			bc.setBcCode(AgencyDict.GLO_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_TRANSMISSION_EXCEPTION));
			return;
		}

		try {
			params = res.split("&");
			for (int i = 0; i < params.length; i++) {
				String[] p = params[i].split("=");
				if (p.length == 2) {
					responseMap.put(p[0], p[1]);
				}
			}
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data disembly exception");
			bc.setBcCode(AgencyDict.GLO_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_TRANSMISSION_EXCEPTION));
			return;
		}

		// 日志打印返回数据
		StringBuffer reve = new StringBuffer();
		for (Map.Entry<String, String> e : responseMap.entrySet()) {
			reve.append(e.getKey() + "=" + e.getValue() + "&");
		}
		LogUtil.agency("GLOEDC_SALES", reve.toString());

		if (!responseMap.containsKey("response.gatewayCode")) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data parameter exception");
			bc.setBcCode(AgencyDict.GLO_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_TRANSMISSION_EXCEPTION));
			return;
		}

		if (!responseMap.get("response.gatewayCode").equals("APPROVED")) {
			bc.setBcMark(StringUtil.space(responseMap.get("response.gatewayCode")));
			bc.setBcInfo(StringUtil.space(responseMap.get("response.acquirerCode")) + StringUtil.quote(responseMap.get("response.acquirerMessage")));
			bc.setBcCode(AgencyDict.GLO_SALES_FAILURE);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.GLO_SALES_FAILURE));
			return;
		}

		bc.setBcState("1");
		bc.setBcRef(bc.getBcNo());
		bc.setBcCard(card.getCrdFake());
		bc.setBcTime(DateUtil.format(new Date(), DateUtil.DATE));
		bc.setBcMark(StringUtil.space(responseMap.get("response.gatewayCode")));
		bc.setBcInfo(StringUtil.space(responseMap.get("response.acquirerCode")) + StringUtil.quote(responseMap.get("response.acquirerMessage")));
		bc.setBcCode(BcDict.BC_SUCCESS);
		bc.setBcMessage(BcDict.MESSAGES.get(BcDict.BC_SUCCESS));
		return;
	}

	public static void main(String[] args) throws Exception {

//		Map<String, String> parameterMap = new TreeMap<String, String>();
//		parameterMap.put("apiUsername", "merchant.TESTOB2018092102");
//		parameterMap.put("apiPassword", "c7753f52f9f3c6e63c1f4466dcc9279b");
//		parameterMap.put("apiOperation", "PAY");
//		parameterMap.put("merchant", "TESTOB2018092102");
//		parameterMap.put("order.amount", "10.11");
//		parameterMap.put("order.currency", "CNY");
//		parameterMap.put("order.id", "111");
//		parameterMap.put("order.reference", "222");
//		parameterMap.put("sourceOfFunds.type", "CARD");
//		parameterMap.put("sourceOfFunds.provided.card.number", "5111111111111118");
//		parameterMap.put("sourceOfFunds.provided.card.expiry.year", "37");
//		parameterMap.put("sourceOfFunds.provided.card.expiry.month", "05");
//		parameterMap.put("sourceOfFunds.provided.card.securityCode", "123");
//		parameterMap.put("transaction.id", "333");
//		parameterMap.put("transaction.reference", "444");
//
//		String res = new HttpUtil().post("https://gpsgh.gateway.mastercard.com/api/nvp/version/45", parameterMap);
//
//		Map responseMap = new HashMap();
//		String[] params = res.split("&");
//		for (int i = 0; i < params.length; i++) {
//			String[] p = params[i].split("=");
//			if (p.length == 2) {
//				responseMap.put(p[0], p[1]);
//			}
//		}
//
//		for (Iterator iter = responseMap.entrySet().iterator(); iter.hasNext();) {
//			Map.Entry e = (Map.Entry) iter.next();
//			System.out.println(e.getKey() + "===" + e.getValue());
//		}

	}
}